﻿// Copyright: 2012 
// Author: Minh Vu - YoungJ
// File name: XmlParser.cs
// Solution: PhoneAppRssReader
// Project: PhoneAppRssReader
// Time: 10:22 AM 15/04/2012

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Xml.Linq;


namespace PhoneAppRssReader.Entity
{
    public class XmlParser
    {
        private static string ConvertString(string source)
        {
            try
            {
                while (source.IndexOf("&#", System.StringComparison.Ordinal) > 0)
                {
                    var index = source.IndexOf("&#", System.StringComparison.Ordinal) + 2;
                    var length = source.IndexOf(";", System.StringComparison.Ordinal) - index;
                    var sChar = source.Substring(index, length);
                    var iChar = int.Parse(sChar);
                    var replace = Convert.ToChar(iChar).ToString(CultureInfo.InvariantCulture);
                    source = source.Replace("&#" + sChar + ";", replace);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
            return source;
        }
        public static ObservableCollection<FeedItem> FeedItemsParser(string xml)
        {
            try
            {
                var list = new ObservableCollection<FeedItem>();
                xml = ConvertString(xml);
                var items = XDocument.Parse(xml);
                var data = from item in items.Descendants("item")
                           select new
                           {
                               Title = (string)item.Element("title"),
                               Description = (string)item.Element("description"),
                               Link = (string)item.Element("link"),
                               PubDate = (string)item.Element("pubDate")
                           };
                foreach (var item in data)
                {
                    list.Add(new FeedItem { Title = item.Title, Description = item.Description, Link = new Uri(item.Link, UriKind.RelativeOrAbsolute), PubDate = Convert.ToDateTime(item.PubDate) });
                }
                return list;
            }
            catch (Exception)
            {
                return null;
            }
        }


        public static ObservableCollection<FeedItem> FeedItemsParser(IEnumerable<SyndicationItem> Items)
        {
            var data = from item in Items
                       select new
                       {
                           title = item.Title.Text,
                           description = item.Summary.Text,
                           link = item.Links[0].Uri,
                           pubDate = item.PublishDate.DateTime
                       };
            var list = new ObservableCollection<FeedItem>();
            foreach (var item in data)
            {
                list.Add(new FeedItem { Title = item.title, Description = item.description, Link = item.link, PubDate = item.pubDate });
            }
            return list;
        }

        public static Collection<SyndicationItem> GetSyndicationItems(string xml)
        {
            try
            {
                xml = ConvertString(xml);
                var items = XDocument.Parse(xml);
                var list = new Collection<SyndicationItem>();
                var synItems = from item in items.Descendants("item")
                               select new
                               {
                                   Title = (string)item.Element("title"),
                                   Description = (string)item.Element("description"),
                                   Link = (string)item.Element("link"),
                                   PubDate = (string)item.Element("pubDate")
                               };
                foreach (var item in synItems)
                {
                    var synItem = new SyndicationItem
                                      {
                                          Title = new TextSyndicationContent(item.Title),
                                          Summary = new TextSyndicationContent(item.Description),
                                          PublishDate = Convert.ToDateTime(item.PubDate)
                                      };
                    synItem.Links.Add(new SyndicationLink(new Uri(item.Link, UriKind.RelativeOrAbsolute)));
                    list.Add(synItem);
                }
                return list;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }

}